package cn.wangzengqiang.study.designpattern.observer.own;

import java.util.ArrayList;
import java.util.List;

/**
 * 逻辑思维 订阅号
 */
public class LogicalThinking implements Subject {
    private List<Observer> observers = new ArrayList<Observer>();
    /**
     * 逻辑思维每日60s主题
     */
    private String msg;

    @Override
    public void registerObserver(Observer observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int i = observers.indexOf(observer);
        if (i >= 0) {
            observers.remove(i);
        }

    }

    /**
     * 主题更新消息
     */
    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(msg);
        }
    }

    public void setMsg(String msg) {
        this.msg = msg;
        notifyObservers();
    }
}
